//
//  JHPropressView.m
//  SHIJIE
//
//  Created by Gufs on 17/5/19.
//  Copyright © 2017年 yjh. All rights reserved.
//

#import "JHPropressView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UIView+Frame.h"
//#import "Common.h"

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define mcColor [UIColor colorWithRed:63/255.0 green:143/255.0 blue:247/255.0 alpha:1]
@interface JHPropressView ()

@property(nonatomic,strong)UIImageView *imgv;

@property(nonatomic,strong)UIView *bgView;

@property(nonatomic,assign)CGPoint startP;

@property(nonatomic,assign)CGPoint endP;

@property(nonatomic,assign)BOOL isFirst;

@property(nonatomic,assign)float tempPress;

@end

@implementation JHPropressView

-(UIView *)bgView{
    if(!_bgView){
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor colorWithWhite:.25 alpha:1];
    }
    return _bgView;
}

-(UIView *)cachePressView{
    if(!_cachePressView){
        _cachePressView = [UIView new];
        _cachePressView.hidden = YES;
        _cachePressView.backgroundColor = [UIColor whiteColor];
    }
    return _cachePressView;
}

-(UIView *)pressView{
    if(!_pressView){
        _pressView = [UIView new];
        _pressView.backgroundColor = mcColor;
    }
    return _pressView;
}

-(float)lineWith{
    if(!_lineWith){
        _lineWith = IS_IPAD?10:5;
    }
    return _lineWith;
}

-(UIImageView *)imgv{
    if (!_imgv) {
        _imgv = [UIImageView new];
    }
    return _imgv;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.bgView];
        [self addSubview:self.cachePressView];
        [self addSubview:self.pressView];
        [self addSubview:self.imgv];
    }
    return self;
}

-(void)setIsCache:(BOOL)isCache{
    _isCache = isCache;
    if(_isCache){
        _cachePressView.hidden = NO;
    }else{
        _cachePressView.hidden = YES;
    }
}

-(void)setCachepress:(float)cachepress{
    _cachepress = cachepress;
    [self setNeedsLayout];
}

-(void)refreshProress:(float)press{
    _press = press;
    [self setNeedsLayout];
}

-(void)setPress:(float)press{
    _press = press;
    [self setNeedsLayout];
}

-(void)setJhImage:(UIImage *)jhImage{
    _jhImage = jhImage;
    _imgv.image = _jhImage;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *t = [touches anyObject];
    _startP = [t locationInView:self];
    _isFirst = YES;
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *t = [touches anyObject];
    _endP = [t locationInView:self];
    CGFloat x = _startP.x - _endP.x;
    CGFloat y = _startP.y - _endP.y;
    if (_isCache?fabs(x)>fabs(y):fabs(x)<fabs(y)) {
        
        if(_isFirst){
            _isFirst = NO;
            _tempPress = _press;
            _press = _isCache?(1-x)/self.zf_width+_press:y/self.zf_height+_press;
            if([self.delegate respondsToSelector:@selector(returnBegan:andIscache:)]){
                [self.delegate returnBegan:YES andIscache:_isCache];
            }
        }else{
            _press = _isCache?(1-x)/self.zf_width+_tempPress:y/self.zf_height+_tempPress;
            
            
            if([self.delegate respondsToSelector:@selector(returnMove:)]){
                [self.delegate returnMove:_press];
            }
        }
        
        if(_press>1){
            _press = 1;
        }else if (_press<0){
            _press = 0;
        }
        
        
        [self setNeedsLayout];
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if([self.delegate respondsToSelector:@selector(returnBegan:andIscache:)]){
        [self.delegate returnBegan:NO andIscache:_isCache];
    }
    if (_press>=0) {
        if (_press<=1) {
            if([self.delegate respondsToSelector:@selector(returnPress:andIscache:)]){
                [self.delegate returnPress:_press andIscache:_isCache];
            }
        }
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    if([self.delegate respondsToSelector:@selector(returnBegan:andIscache:)]){
        [self.delegate returnBegan:NO andIscache:_isCache];
    }
    if (_press>=0) {
        if (_press<=1) {
            if([self.delegate respondsToSelector:@selector(returnPress:andIscache:)]){
                [self.delegate returnPress:_press andIscache:_isCache];
            }
        }
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    if(isnan(_press)){
        _press = 0;
    }
    
    if(isinf(_press)){
        _press = 0;
    }
    
    if(isnan(_cachepress)){
        _cachepress = 0;
    }
    
    if(isinf(_cachepress)){
        _cachepress = 0;
    }
    
    float viewWidth = self.lineWith;

    if(_isCache){
        _bgView.frame = CGRectMake(0, (self.zf_height-viewWidth)/2, self.zf_width, viewWidth);
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = viewWidth/2.0;
        
        _cachePressView.frame = CGRectMake(0, (self.zf_height-viewWidth)/2, self.zf_width*_cachepress, viewWidth);
        _cachePressView.layer.masksToBounds = YES;
        _cachePressView.layer.cornerRadius = viewWidth/2.0;
        
        _pressView.frame = CGRectMake(0, (self.zf_height-viewWidth)/2, self.zf_width*_press , viewWidth);
        _pressView.layer.masksToBounds = YES;
        _pressView.layer.cornerRadius = viewWidth/2.0;
        
        _imgv.frame = CGRectMake(self.zf_width*_press-(IS_IPAD?15:10),(self.zf_height-(IS_IPAD?30:20))/2, IS_IPAD?30:20, IS_IPAD?30:20);
    }else{
        _bgView.frame = CGRectMake((self.zf_width-viewWidth)/2, 0, viewWidth, self.zf_height);
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = viewWidth/2.0;
        
        _pressView.frame = CGRectMake((self.zf_width-viewWidth)/2, self.zf_height*(1.0-_press), viewWidth, self.zf_height-(self.zf_height*(1.0-_press)));
        _pressView.layer.masksToBounds = YES;
        _pressView.layer.cornerRadius = viewWidth/2.0;
        
        _imgv.frame = CGRectMake((self.zf_width-(IS_IPAD?30:20))/2, self.zf_height*(1.0-_press)-(IS_IPAD?15:10), IS_IPAD?30:20, IS_IPAD?30:20);
    }
}

@end
